package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"net/http"
)

// main
//
//	@Description: gin 快速开始
func main() {
	// 初始化 Viper
	viper.SetConfigName("config.yaml") // 配置文件名称（不带扩展名）
	viper.SetConfigType("yaml")        // 配置文件类型
	viper.AddConfigPath(".")           // 配置文件路径
	if err := viper.ReadInConfig(); err != nil {
		panic(fmt.Errorf("failed to read config file: %v", err))
	}
	port := viper.GetString("server.port")

	//创建一个默认路由引擎
	engine := gin.Default()

	//配置路由, GET POST PUT DELETE
	engine.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "hello gin-two-config",
			"code":    200,
			"data":    nil,
		})
	})

	engine.GET("/get", func(c *gin.Context) {
		c.String(http.StatusOK, "%s", "get success")
	})

	engine.POST("/add", func(c *gin.Context) {
		c.String(200, "%s", "add success")
	})

	engine.PUT("/edit", func(c *gin.Context) {
		c.String(http.StatusOK, "%s", "edit success")
	})
	engine.DELETE("/del", func(c *gin.Context) {
		c.String(200, "%s", "del success")
	})

	//启动服务
	engine.Run("0.0.0.0:" + port)

}
